<template>
  <div>
    <h1>商品类型列表</h1>
    <div>
      <el-form :inline="true" :model="listFrom" class="demo-form-inline">
        <el-form-item label="类型名称">
          <el-input
            v-model="listFrom.keyWord"
            placeholder="请输入类型名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="类型状态">
          <el-select
            v-model="listFrom.typeId"
            placeholder="请输入状态"
            clearable
            style="width: 160px"
          >
            <el-option label="显示" value="0" />
            <el-option label="隐藏" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="list()">查询</el-button>
          <el-button type="danger" @click="onAddTypeList()">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <el-table-column prop="id" label="id" sortable />
      <el-table-column prop="name" label="分类名称" sortable />
      <el-table-column prop="typeImage" label="分类图片" sortable>
        <template #default="scope">
          <el-avatar
            v-if="scope.row.typeImage != null"
            :size="50"
            :src="scope.row.typeImage"
          />
        </template>
      </el-table-column>
      <el-table-column prop="typeTop" label="排序" sortable />
      <el-table-column prop="typeStatus" label="状态" sortable>
        <template #default="scope">
          <el-tag v-if="scope.row.typeStatus == 0">显示</el-tag>
          <el-tag v-if="scope.row.typeStatus == 1" type="danger">隐藏</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" />
    </el-table>
    <div>
      <el-dialog v-model="dialogFormVisible" title="添加类型页面" width="500">
        <el-form
          :model="form"
          :rules="rules"
          ref="ruleFormRef"
          label-width="auto"
        >
          <el-form-item
            label="分类名称"
            :label-width="formLabelWidth"
            prop="typeName"
          >
            <el-input v-model="form.typeName" autocomplete="off" />
          </el-form-item>
          <el-form-item label="分类图片" :label-width="formLabelWidth">
            <el-upload
              class="avatar-uploader"
              action="http://localhost:10010/prod/type/uploadFile"
              :headers="headers"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
          <el-form-item
            label="排序"
            :label-width="formLabelWidth"
            prop="typeTop"
          >
            <el-input-number
              v-model="form.typeTop"
              :min="1"
              :max="100000"
              @change="handleChange"
            />
          </el-form-item>
          <el-form-item
            label="状态"
            :label-width="formLabelWidth"
            prop="typeStatus"
          >
            <el-radio-group v-model="form.typeStatus">
              <el-radio value="0" size="large">显示</el-radio>
              <el-radio value="1" size="large">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              添加
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import { getTypeList, addTypeList } from "../api/api";
import { ElMessage } from "element-plus";
import { Plus } from "@element-plus/icons-vue";

import type { UploadProps } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
const imageUrl = ref("");
const tableData = ref([]);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const listFrom = ref({
  pageNum: 1,
  pageSize: 2,
  typeId: null,
  keyWord: "",
});
const form = ref({
  typeName: "",
  typeImage: "",
  typeTop: null,
  typeStatus: 0,
});
const ruleFormRef = ref<FormInstance>();
const handleChange = (value: number | undefined) => {
  console.log(value);
};
const headers = ref({
  token: window.sessionStorage.getItem("token"),
});
//列表
const list = () => {
  getTypeList(listFrom.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data;
    }
  });
};
//添加
const onAddTypeList = () => {
  dialogFormVisible.value = true;
};
//图片上传
const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  imageUrl.value = response.data;
};
//校验
const rules = reactive<FormRules>({
  typeName: [{ required: true, message: "请输入类型名称", trigger: "blur" }],
  typeTop: [{ required: true, message: "请输入排序", trigger: "blur" }],
  typeStatus: [{ required: true, message: "请选择类型状态", trigger: "blur" }],
  name: [
    { required: true, message: "Please input Activity name", trigger: "blur" },
    { min: 3, max: 5, message: "Length should be 3 to 5", trigger: "blur" },
  ],
});
//添加
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      form.value.typeImage = imageUrl.value;
      addTypeList(form.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("添加成功");
          dialogFormVisible.value = false;
          list();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};
onMounted(() => {
  list();
});
</script>
<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
